part of '../repository.dart';

const _kIssueWithinAppTransactionRevocationReasonValue = 'issue_within_app';
const _kOtherReasonTransactionRevocationReasonValue = 'other_reason';

class _TransactionRevocationReasonDto extends Equatable {
  const _TransactionRevocationReasonDto._internal({
    required this.revocationReasonString,
  });

  final String revocationReasonString;

  @override
  List<Object?> get props {
    return [
      revocationReasonString,
    ];
  }

  factory _TransactionRevocationReasonDto.fromString(String revocationReasonString) {
    return _TransactionRevocationReasonDto._internal(
      revocationReasonString: revocationReasonString,
    );
  }

  factory _TransactionRevocationReasonDto.fromEntity(
      AppStoreTransactionRevocationReason revocationReason) {
    late final String revocationReasonString;

    if (revocationReason == AppStoreTransactionRevocationReason.issueWithinApp) {
      revocationReasonString = _kIssueWithinAppTransactionRevocationReasonValue;
    } else if (revocationReason == AppStoreTransactionRevocationReason.otherReason) {
      revocationReasonString = _kOtherReasonTransactionRevocationReasonValue;
    }

    return _TransactionRevocationReasonDto._internal(
      revocationReasonString: revocationReasonString,
    );
  }

  @override
  String toString() {
    return revocationReasonString;
  }

  AppStoreTransactionRevocationReason toEntity() {
    if (revocationReasonString == _kIssueWithinAppTransactionRevocationReasonValue) {
      return AppStoreTransactionRevocationReason.issueWithinApp;
    }

    if (revocationReasonString == _kOtherReasonTransactionRevocationReasonValue) {
      return AppStoreTransactionRevocationReason.otherReason;
    }
    throw UnimplementedError();
  }
}
